package com.gxa.meiye.admin.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.meiye.core.common.JsonResult;
import com.gxa.meiye.core.pojo.vo.TherapistLoginInfo;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author chen
 * @date 2022/7/29 13:43
 */
@Component
public class TokenUtil {

    @Resource
    private RestTemplate restTemplate;


    public Map getUserByToken(String token) throws JsonProcessingException {
        String url = "http://1.117.60.194:8082/mlstherapist/checkToken";
        //请求头（包括请求类型）
        HttpHeaders httpHeaders = new HttpHeaders();
        //MediaType.APPLICATION_FORM_URLENCODED对应于 @RequestParam传参
        //MediaType.APPLICATION_JSON 对应 @RequestBody传参
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //请求参数
        //Spring提供的工具类可以一次传递多个参数
        //服务器之间传参将token以参数传递
        MultiValueMap<String,String> multiValueMap = new LinkedMultiValueMap<>();
        multiValueMap.add("token",token);
        //请求类型
        //HttpEntity:Spring提供的一个传递http请求的实体类型
        HttpEntity httpEntity = new HttpEntity(multiValueMap,httpHeaders);
        //通过post方法发送请求
        Map map = restTemplate.postForObject(url, httpEntity, Map.class);


        //将user对象转换为json返回前端,将拦截器里的对象放在threadLocal里，而不应该直接返回给前端
        LinkedHashMap data =(LinkedHashMap) map.get("data");
        return data;
    }

}
